package com.manager.service.impl;


import com.manager.bean.Manager;
import com.manager.bean.ManagerExample;
import com.manager.bean.PageInfo;
import com.manager.mapper.ManagerMapper;
import com.manager.service.ManagerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Manager)表服务实现类
 *
 * @author makejava
 * @since 2021-03-02 11:00:24
 */
@Service("managerService")
public class ManagerServiceImpl implements ManagerService {
    @Resource
    private ManagerMapper managerDao;

    @Override
    public PageInfo<Manager> findPageInfo(Integer Id, String Name, Integer pageIndex, Integer pageSize) {
        PageInfo<Manager> pi = new PageInfo<Manager>();
        pi.setPageIndex(pageIndex);
        pi.setPageSize(pageSize);
        ManagerExample ManagerExample=new ManagerExample();
        ManagerExample.Criteria criteria=ManagerExample.createCriteria();
        if(Name!=null&&Name!=""){
            criteria.andNameLike("%"+Name+"%");
        }
        if(Id!=null){
            criteria.andIdEqualTo(Id);
        }
        if(Name!=null&&Name!=""){
            criteria.andNameEqualTo(Name);
        }
        //获取总条数
        Integer totalCount = managerDao.countByExample(ManagerExample);
        if (totalCount>0){
            pi.setTotalCount(totalCount);
            //每一页显示学生信息数
            //currentPage = (pageIndex-1)*pageSize  当前页码数减1*最大条数=开始行数
            List<Manager> Managers = managerDao.selectByExample(ManagerExample);
            int start=(pi.getPageIndex()-1)*pi.getPageSize();
            int end=start+pi.getPageSize();
            if(end>Managers.size()-1){
                end=Managers.size();
            }
            Managers=Managers.subList(start,end);
            pi.setList(Managers);
        }
        return pi;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Manager queryById(Integer id) {
        return this.managerDao.selectByPrimaryKey(id);
    }


    @Override
    public List<Manager> getAll(){
        ManagerExample managerExample = new ManagerExample();
        ManagerExample.Criteria criteria = managerExample.createCriteria();
        return managerDao.selectByExample(managerExample);
    }

    /**
     * 新增数据
     *
     * @param manager 实例对象
     * @return 实例对象
     */
    @Override
    public Manager insert(Manager manager) {
        this.managerDao.insert(manager);
        return manager;
    }

    /**
     * 修改数据
     *
     * @param manager 实例对象
     * @return 实例对象
     */
    @Override
    public Manager update(Manager manager) {
        this.managerDao.updateByPrimaryKey(manager);
        return this.queryById(manager.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        this.managerDao.deleteByPrimaryKey(id);
        return true;
    }
}
